package cn.itsource.blog.controller;

import cn.itsource.blog.anno.PreAuthorize;
import cn.itsource.blog.domain.Employee;
import cn.itsource.blog.query.ArticleQuery;
import cn.itsource.blog.service.IEmployeeService;
import cn.itsource.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @PatchMapping
    public JsonResult queryPage(@RequestBody ArticleQuery employeeQuery) {
        try{
            return JsonResult.success(employeeService.pageInfo(employeeQuery));
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }

    @PutMapping
    @PreAuthorize(name = "文章新增",sn = "employee:save",descs = "就是测试一把")
    public JsonResult save(@RequestBody Employee employee) {
        try{
            employeeService.save(employee);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }



    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id) {
        try{
            employeeService.delete(id);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }

    @PatchMapping("/batchDelete")
    public JsonResult batchDelete(@RequestBody List<Employee> users){
        try{
            employeeService.batchDelete(users);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }

  


}
